package org.ytor.core.sqlflow.executor;

import java.util.List;

/**
 * created by yangtong on 2025/8/8 18:20:34
 * <br/>
 */
public class SQLRuntime {
    /**
     * SQL 执行执行器
     */
    private static volatile SqlExecutor EXECUTOR;

    private SQLRuntime() {
        throw new UnsupportedOperationException();
    }

    public static void setExecutor(SqlExecutor ex) {
        if (EXECUTOR != null) {
            throw new IllegalStateException("SQL 执行器已存在");
        }
        EXECUTOR = ex;
    }

    /**
     * 执行 SELECT
     */
    public static DQLResult doSelect(String sql, List<Object> params) {
        // 简化SQL字符串
        sql = sql.replaceAll("\t", " ").replace("\n", " ").replaceAll("\\s+", " ").trim();
        return EXECUTOR.doQuery(sql, params);
    }

    /**
     * 执行 INSERT
     */
    public static DMLResult doInsert(String sql, List<Object> params) {
        // 简化SQL字符串
        sql = sql.replaceAll("\t", " ").replace("\n", " ").replaceAll("\\s+", " ").trim();
        return EXECUTOR.doInsert(sql, params);
    }

    /**
     * 执行 DELETE
     */
    public static DMLResult doDelete(String sql, List<Object> params) {
        // 简化SQL字符串
        sql = sql.replaceAll("\t", " ").replace("\n", " ").replaceAll("\\s+", " ").trim();
        return EXECUTOR.doDelete(sql, params);
    }

    /**
     * 执行 UPDATE
     */
    public static DMLResult doUpdate(String sql, List<Object> params) {
        // 简化SQL字符串
        sql = sql.replaceAll("\t", " ").replace("\n", " ").replaceAll("\\s+", " ").trim();
        return EXECUTOR.doUpdate(sql, params);
    }

    /**
     * 批量执行 INSERT
     */
    public static List<DMLResult> doInsertBatch(String sql, List<List<Object>> batchParams) {
        // 简化SQL字符串
        sql = sql.replaceAll("\t", " ").replace("\n", " ").replaceAll("\\s+", " ").trim();
        return EXECUTOR.doInsertBatch(sql, batchParams);
    }
}
